import { menuAPI } from '@/api/menu'

// 菜单API测试工具
export const menuTest = {
  // 测试获取当前用户菜单
  async testGetCurrentUserMenu() {
    console.log('🧪 测试获取当前用户菜单...')
    try {
      const result = await menuAPI.getCurrentUserMenu()
      console.log('✅ 获取当前用户菜单成功:', result)
      return result
    } catch (error) {
      console.error('❌ 获取当前用户菜单失败:', error)
      return null
    }
  },

  // 测试根据用户ID获取菜单
  async testGetUserMenu(userId = 1) {
    console.log(`🧪 测试获取用户${userId}的菜单...`)
    try {
      const result = await menuAPI.getUserMenu(userId)
      console.log('✅ 获取用户菜单成功:', result)
      return result
    } catch (error) {
      console.error('❌ 获取用户菜单失败:', error)
      return null
    }
  },

  // 测试获取所有菜单树
  async testGetAllMenuTree() {
    console.log('🧪 测试获取所有菜单树...')
    try {
      const result = await menuAPI.getAllMenuTree()
      console.log('✅ 获取所有菜单树成功:', result)
      return result
    } catch (error) {
      console.error('❌ 获取所有菜单树失败:', error)
      return null
    }
  },

  // 测试根据权限ID构建菜单树
  async testBuildMenuTree(permissionIds = [1, 2, 5, 6, 10]) {
    console.log('🧪 测试根据权限ID构建菜单树...')
    try {
      const result = await menuAPI.buildMenuTree(permissionIds)
      console.log('✅ 构建菜单树成功:', result)
      return result
    } catch (error) {
      console.error('❌ 构建菜单树失败:', error)
      return null
    }
  },

  // 运行所有菜单测试
  async runAllTests() {
    console.log('🚀 开始菜单API测试...')
    
    // 测试获取当前用户菜单
    await this.testGetCurrentUserMenu()
    
    // 测试根据用户ID获取菜单
    await this.testGetUserMenu(1)
    
    // 测试获取所有菜单树
    await this.testGetAllMenuTree()
    
    // 测试构建菜单树
    await this.testBuildMenuTree()
    
    console.log('✨ 菜单API测试完成')
  },

  // 真实菜单数据（来自后端API）
  getRealMenuData() {
    return {
      "code": 200,
      "msg": "success",
      "data": {
        "userId": 1,
        "username": "admin",
        "menuTree": [
          {
            "id": 100,
            "name": "仪表盘",
            "code": "dashboard",
            "type": 1,
            "path": "/dashboard",
            "icon": "dashboard",
            "sort": 0,
            "status": 1,
            "children": [],
            "hasChildren": false,
            "level": 0,
            "isLeaf": true,
            "fullPath": "/dashboard"
          },
          {
            "id": 1,
            "name": "系统管理",
            "code": "system:manage",
            "type": 1,
            "path": "/system",
            "icon": "setting",
            "sort": 1,
            "status": 1,
            "children": [
              {
                "id": 2,
                "name": "用户管理",
                "code": "user:manage",
                "type": 1,
                "parentId": 1,
                "path": "/system/user",
                "icon": "user",
                "sort": 1,
                "status": 1,
                "children": [],
                "hasChildren": false,
                "level": 1,
                "isLeaf": true,
                "fullPath": "/system/user",
                "parentPath": "/system"
              },
              {
                "id": 3,
                "name": "角色管理",
                "code": "role:manage",
                "type": 1,
                "parentId": 1,
                "path": "/system/role",
                "icon": "usergroup-add",
                "sort": 2,
                "status": 1,
                "children": [],
                "hasChildren": false,
                "level": 1,
                "isLeaf": true,
                "fullPath": "/system/role",
                "parentPath": "/system"
              },
              {
                "id": 4,
                "name": "权限管理",
                "code": "permission:manage",
                "type": 1,
                "parentId": 1,
                "path": "/system/permission",
                "icon": "safety-certificate",
                "sort": 3,
                "status": 1,
                "children": [],
                "hasChildren": false,
                "level": 1,
                "isLeaf": true,
                "fullPath": "/system/permission",
                "parentPath": "/system"
              },
              {
                "id": 5,
                "name": "分组管理",
                "code": "group:manage",
                "type": 1,
                "parentId": 1,
                "path": "/system/group",
                "icon": "apartment",
                "sort": 4,
                "status": 1,
                "children": [],
                "hasChildren": false,
                "level": 1,
                "isLeaf": true,
                "fullPath": "/system/group",
                "parentPath": "/system"
              }
            ],
            "hasChildren": true,
            "level": 0,
            "isLeaf": false,
            "fullPath": "/system"
          },
          {
            "id": 101,
            "name": "个人中心",
            "code": "profile",
            "type": 1,
            "path": "/profile",
            "icon": "user-circle",
            "sort": 999,
            "status": 1,
            "children": [],
            "hasChildren": false,
            "level": 0,
            "isLeaf": true,
            "fullPath": "/profile"
          }
        ],
        "permissions": [
          "system:manage",
          "user:manage",
          "role:manage",
          "permission:manage",
          "group:manage",
          "user:view",
          "user:add",
          "user:edit",
          "user:delete",
          "dashboard",
          "profile"
        ],
        "roles": [
          "ROLE_SUPER_ADMIN"
        ]
      }
    }
  },

  // 模拟菜单数据（用于开发测试）
  getMockMenuData() {
    return this.getRealMenuData().data
  },

  // 使用模拟数据测试菜单渲染
  testMenuRendering() {
    console.log('🧪 测试菜单渲染...')
    const mockData = this.getMockMenuData()
    console.log('📋 模拟菜单数据:', mockData)
    
    // 测试菜单树结构
    const testMenuTree = (tree, level = 0) => {
      tree.forEach(menu => {
        console.log(`${'  '.repeat(level)}📁 ${menu.name} (${menu.code}) - ${menu.path}`)
        if (menu.hasChildren && menu.children.length > 0) {
          testMenuTree(menu.children, level + 1)
        }
      })
    }
    
    console.log('🌳 菜单树结构:')
    testMenuTree(mockData.menuTree)
    
    console.log('🔑 用户权限:', mockData.permissions)
    console.log('👤 用户角色:', mockData.roles)
  }
}

// 在开发环境下将测试工具挂载到window对象
if (import.meta.env.DEV) {
  window.menuTest = menuTest
}